//
//  PhotoSelectCVC.m
//  照片选择器
//
//  Created by WONG on 16/3/3.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "PhotoSelectCVC.h"
#import "PhotoSelectCell.h"

// 选择照片最大数
#define maxPhotoCount 9

@interface PhotoSelectCVC ()<PhotoSelectCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

/**
 *  用户选择的照片数组
 */
@property (nonatomic,strong) NSMutableArray *phonos;
/**
 *  记录被点击的cell
 */
@property (nonatomic,strong) NSIndexPath *currentIndexPath;


@end


@implementation PhotoSelectCVC

- (instancetype)init {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(80, 80);
    layout.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    
    return [super initWithCollectionViewLayout:layout];
}



static NSString * const reuseIdentifier = @"photoSelectCell";

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register cell classes
    [self.collectionView registerClass:[PhotoSelectCell class] forCellWithReuseIdentifier:reuseIdentifier];
   
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.phonos.count < maxPhotoCount ? self.phonos.count + 1 : self.phonos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoSelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    
    if (indexPath.item < self.phonos.count) {
        cell.image = self.phonos[indexPath.item];
    }else {
        // 调用这个方法的原因是，重新设置图片，因为有缓存
        [cell setAddButton];
    }
    

    //cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    
    return cell;
}


#pragma mark - PhotoSelectCellDelegate
- (void)photoSelectCell:(PhotoSelectCell *)cell addButtonClick:(UIButton *)buttton {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        NSLog(@"无法访问照片库");
        return;
    }
    self.currentIndexPath = [self.collectionView indexPathForCell:cell];
    
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.delegate = self;
    
    [self presentViewController:pickerController animated:YES completion:nil];
}
- (void)photoSelectCell:(PhotoSelectCell *)cell removeButtonClick:(UIButton *)buttton {
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    [self.phonos removeObjectAtIndex:indexPath.item];
    [self.collectionView reloadData];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
    // 缩放后的图片
    UIImage *newImage = image;
    // 判断点击的是图片还是加号按钮
    // currentIndexPath.item = photos.count 点击的就是最后一个
    if (self.currentIndexPath.item == self.phonos.count
        ) {
        // 点击的是加号按钮
        // 将用户选择的图片添加到数组
        [self.phonos addObject:newImage];
    }else {
        // 点击的是图片,替换对应的图片
        self.phonos[self.currentIndexPath.item] = newImage;
    }
    [self.collectionView reloadData];
    // 关闭系统照片库
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - lazy load
- (NSMutableArray *)phonos {
    if (_phonos == nil) {
        _phonos = [NSMutableArray array];
    }
    return _phonos;
}


@end

























